PCI: Lock down BAR access when the kernel is locked down
authorMatthew Garrett <matthew.garrett@nebula.com>
Wed, 5 Apr 2017 16:40:30 +0000 (17:40 +0100)
committerSalvatore Bonaccorso <carnil@debian.org>
Sun, 15 Oct 2017 06:57:36 +0000 (07:57 +0100)
Any hardware that can potentially generate DMA has to be locked down in
order to avoid it being possible for an attacker to modify kernel code,
allowing them to circumvent disabled module loading or module signing.
Default to paranoid - in future we can potentially relax this for
sufficiently IOMMU-isolated devices.

Signed-off-by: Matthew Garrett <matthew.garrett@nebula.com>
Signed-off-by: David Howells <dhowells@redhat.com>
[bwh: For 4.12, adjust context]

Gbp-Pq: Topic features/all/lockdown
Gbp-Pq: Name 0049-PCI-Lock-down-BAR-access-when-the-kernel-is-locked-d.patch

drivers/pci/pci-sysfs.c
drivers/pci/proc.c
drivers/pci/syscall.c

index 2f3780b50723675e7e595a85298848d54f08f858..534d6df4aec23812e2f35ef92fd76edb7c8c45fc 100644 (file)
@@ -881,6 +881,9 @@ static ssize_t pci_write_config(struct file *filp, struct kobject *kobj,
        loff_t init_off = off;
        u8 *data = (u8 *) buf;
 
+       if (kernel_is_locked_down())
+               return -EPERM;
+
        if (off > dev->cfg_size)
                return 0;
        if (off + count > dev->cfg_size) {
@@ -1175,6 +1178,9 @@ static int pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
        enum pci_mmap_state mmap_type;
        struct resource *res = &pdev->resource[bar];
 
+       if (kernel_is_locked_down())
+               return -EPERM;
+
        if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
                return -EINVAL;
 
@@ -1258,6 +1264,9 @@ static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj,
                                     struct bin_attribute *attr, char *buf,
                                     loff_t off, size_t count)
 {
+       if (kernel_is_locked_down())
+               return -EPERM;
+
        return pci_resource_io(filp, kobj, attr, buf, off, count, true);
 }
 
index 098360d7ff81639c90e6ec9bcc0fe56a2a01fd7d..ef16fccb19235385a9ae9fe6850d75dea6337083 100644 (file)
@@ -116,6 +116,9 @@ static ssize_t proc_bus_pci_write(struct file *file, const char __user *buf,
        int size = dev->cfg_size;
        int cnt;
 
+       if (kernel_is_locked_down())
+               return -EPERM;
+
        if (pos >= size)
                return 0;
        if (nbytes >= size)
@@ -195,6 +198,9 @@ static long proc_bus_pci_ioctl(struct file *file, unsigned int cmd,
 #endif /* HAVE_PCI_MMAP */
        int ret = 0;
 
+       if (kernel_is_locked_down())
+               return -EPERM;
+
        switch (cmd) {
        case PCIIOC_CONTROLLER:
                ret = pci_domain_nr(dev->bus);
@@ -236,7 +242,7 @@ static int proc_bus_pci_mmap(struct file *file, struct vm_area_struct *vma)
        struct pci_filp_private *fpriv = file->private_data;
        int i, ret, write_combine = 0, res_bit = IORESOURCE_MEM;
 
-       if (!capable(CAP_SYS_RAWIO))
+       if (!capable(CAP_SYS_RAWIO) || kernel_is_locked_down())
                return -EPERM;
 
        if (fpriv->mmap_state == pci_mmap_io) {
index 9bf993e1f71e837faebe8227accb6a7225a3d7cc..c09524738ceba602dc1883ea17e121d48884c000 100644 (file)
@@ -92,7 +92,7 @@ SYSCALL_DEFINE5(pciconfig_write, unsigned long, bus, unsigned long, dfn,
        u32 dword;
        int err = 0;
 
-       if (!capable(CAP_SYS_ADMIN))
+       if (!capable(CAP_SYS_ADMIN) || kernel_is_locked_down())
                return -EPERM;
 
        dev = pci_get_bus_and_slot(bus, dfn);